package com.dh.service.impl;

import com.dh.core.config.Constant;
import com.dh.entity.UserTax;
import com.dh.entity.vo.UserTaxVO;
import com.dh.repository.UserTaxRepository;
import com.dh.service.UserService;
import com.dh.service.UserTaxService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class UserTaxServiceImpl implements UserTaxService {

    @Autowired
    private UserTaxRepository taxRepository;

    @Autowired
    private UserService userService;

    @Override
    public Page<UserTaxVO> page(PageRequest pageRequest) {
        return taxRepository.findByDeleted(Constant.NOT_DELETE, pageRequest).map(tax -> {
            UserTaxVO vo = new UserTaxVO();
            BeanUtils.copyProperties(tax, vo);
            vo.setUser(userService.get(tax.getUserId()));
            return vo;
        });
    }

    @Override
    public UserTax get(Long taxId) {
        return taxRepository.findOne(taxId);
    }

    @Override
    public void delete(Long taxId) {
        //        UserTax tax = get(taxId);
        //        tax.setDeleted(Constant.DELETE);
        //        taxRepository.save(tax);
        taxRepository.delete(taxId);
    }

    @Override
    public void create(UserTax tax) {
        taxRepository.save(tax);
    }

    @Override
    public void update(UserTax tax) {
        taxRepository.save(tax);
    }

    @Override
    public UserTax findByNoAndDeleted(String no, byte deleted) {
        return taxRepository.findByNoAndDeleted(no, deleted);
    }

    @Override
    public void deleteByUserId(Long userId) {
        taxRepository.deleteByUserId(userId);
    }
}
